import java.util.Arrays;

public class Offer33 {
    public boolean verifyPostorder(int[] postorder) {
        if (postorder==null||postorder.length<=0){
            return true;
        }
        return recursion(postorder, postorder.length);
    }
    public boolean recursion(int[] postorder,int lenght){
        if (postorder==null||lenght<=0){
            return true;
        }
        int root = postorder[lenght-1];
        int i=0;
        for (; i <lenght-1; i++) {
            if (postorder[i]>root) break;
        }
        int j =i;
        for (; j < lenght-1; j++) {
            if (postorder[j]<root) return false;
        }
        boolean left  =true;
        if (i>0) left = recursion(postorder,i);
        boolean right = true;
        if (i<lenght-1) right = recursion(Arrays.copyOfRange(postorder,i,j),lenght-i-1);

        return left&&right;
    }
}
